
package com.platform.config;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Binary Wang
 */
@Configuration
@ConditionalOnClass(WxPayService.class)
@EnableConfigurationProperties(WxPayProperties.class)
public class WxPayConfiguration {
    private WxPayProperties properties;

    @Autowired
    public WxPayConfiguration(WxPayProperties properties) {
        this.properties = properties;
    }

    @Bean
    @ConditionalOnMissingBean
    public WxPayService wxService() {
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(wxPayConfig());
        return wxPayService;
    }

    @Bean
    public WxPayConfig wxPayConfig() {
//        WxPayConfig payConfig = new WxPayConfig();
//        payConfig.setAppId(StringUtils.trimToNull(this.properties.getAppId()));
//        payConfig.setMchId(StringUtils.trimToNull(this.properties.getMchId()));
//        payConfig.setMchKey(StringUtils.trimToNull(this.properties.getMchKey()));
//        payConfig.setSubAppId(StringUtils.trimToNull(this.properties.getSubAppId()));
//        payConfig.setSubMchId(StringUtils.trimToNull(this.properties.getSubMchId()));
//        payConfig.setKeyPath(StringUtils.trimToNull(this.properties.getKeyPath()));
//        payConfig.setKeyContent(this.properties.getKeyContent());
//        // 可以指定是否使用沙箱环境
//        payConfig.setUseSandboxEnv(false);
//
//        return payConfig;
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(StringUtils.trimToNull(this.properties.getAppId()));
        payConfig.setMchId(StringUtils.trimToNull(this.properties.getMchId()));
        payConfig.setMchKey(StringUtils.trimToNull(this.properties.getMchKey()));
        payConfig.setSubAppId(StringUtils.trimToNull(this.properties.getSubAppId()));
        payConfig.setSubMchId(StringUtils.trimToNull(this.properties.getSubMchId()));
        payConfig.setKeyPath(StringUtils.trimToNull(this.properties.getKeyPath()));
        payConfig.setApiV3Key(StringUtils.trimToNull(this.properties.getMchKey()));
        payConfig.setPrivateCertPath(StringUtils.trimToNull(this.properties.getPrivateCertPath()));
        /*todo 暂时未加该密钥地址*/
//        payConfig.setPrivateCertPath();
        payConfig.setPrivateKeyPath(StringUtils.trimToNull(this.properties.getKeyPath()));
        payConfig.setKeyContent(this.properties.getKeyContent());
        // 可以指定是否使用沙箱环境
        payConfig.setUseSandboxEnv(false);

        if(StringUtils.isNotBlank(this.properties.getProxyIp())){
            payConfig.setHttpProxyHost(this.properties.getProxyIp());
            payConfig.setHttpProxyPort(this.properties.getProxyPort());
        }
        return payConfig;
    }
}
